﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.ServiceModel;
using System.Text;
using WebServer.Biz;
using WebServer.Models;

namespace WebServer
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Push”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 Push.svc 或 Push.svc.cs，然后开始调试。
    public class Push : IPush
    {

        /// <summary>
        /// 创建关注
        /// </summary>
        /// <param name="ID">文章ID</param>
        /// <param name="Type"></param>
        /// <param name="UserID">用户</param>
        /// <param name="Code">藏品采用</param>
        /// <returns></returns>
        public string SendPush(int ID, string Type, int UserID, string Code)
        {
            //Type:News,Activity,User,Live
            MsgEntity request = new MsgEntity();
            //推送配置
            var obj = new JObject();
            obj["type"] = 1;
            obj["platform"] = 0;


            //新闻配置
            if (Type == "News")
            {
                News News = new News();
                News = new NewsBiz().NewsByID(ID);
                string sh = News.Shortened;
                if (string.IsNullOrEmpty(sh))
                {
                    obj["title"] =  News.Title;
                }
                else {
                    obj["title"] = "[" + News.Shortened + "]" + News.Title;
                }
               
                obj["content"] = "N_" + News.ID;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
                else
                {
                    if (News.Name == "")
                    {

                        obj["groupName"] = "all";
                    }
                    else
                    {
                        obj["groupName"] = News.Name;
                    }

                }
            }


            //藏品
            if (Type == "Markets")
            {
                Goods Markets = new Goods();
                Markets = new BourseBiz().GoodsByCenterIDAndCode(ID, Code);
                obj["title"] = "[" + Markets.CenterName + "]" + Markets.Fullname;
                obj["content"] = "M_" + ID + "_" + Code;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
                else
                {
                    return " { status:0}";
                }
            }

            //Activity
            if (Type == "Activity")
            {
                Activity Activity = new Activity();
                Activity = new UserBiz().ActivityByID(ID);
                obj["title"] = "[活动]" + Activity.Title;
                obj["content"] = "A_" + Activity.ID;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
                else
                {
                    if (Activity.CenterName == "")
                    {
                        obj["groupName"] = "all";
                    }
                    else
                    {
                        obj["groupName"] = Activity.CenterName;
                    }
                }
            }

            //User
            if (Type == "User")
            {
                User User = new User();
                User = new UserBiz().UserByID(ID);
                if (User.ID>0)
                {
                    obj["title"] = "[会员]" + User.Name;
                    obj["content"] = "U_" + User.ID;
                    if (UserID > 0)
                    {
                        obj["userIds"] = UserID.ToString();
                    }
                }
                else {
                    return " { status:0}";
                }
            }
            //邮吧
            if (Type == "Bar")
            {
                Bar Live = new Bar();
                Live = BarBiz.GetBarByID(ID, 0);
                obj["title"] = "[邮吧]" + Live.Text;
                obj["content"] = "B_" + Live.ID;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
            }
            //Live
            if (Type == "Live")
            {
                Bar Live = new Bar();
                Live = BarBiz.GetBarByID(ID, 0);
                obj["title"] = "[直播]" + Live.Text;
                obj["content"] = "L_" + Live.ID;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
            }
            //网址
            if (Type == "Web")
            {
                obj["title"] =  "推广";
                obj["content"] = "W_" + Code;
                if (UserID > 0)
                {
                    obj["userIds"] = UserID.ToString();
                }
            }
            //配置
            var PushApi = new PushApi("A6981971137420", "9C97363B-E9AB-5225-54CE-3350B772D5F5");

            var ret = PushApi.Message(obj.ToString());

            string result = JObject.Parse(ret).ToString();

            
            //新闻配置
            if (Type == "News")
            {
                if (result == "{\r\n  \"status\": 1\r\n}")
                {
                    bool a = new NewsBiz().NewsAction(ID, "IsPush", true);
                }
            }
            return JObject.Parse(ret).ToString();
        }
    }
}
